import type { UserConfigExport } from "@tarojs/cli"

export default {
  plugins: ['@tarojs/plugin-html'],
  compiler: {
    type: 'webpack5',
    prebundle: {
      exclude: ['taro-ui'],
      enable: false,
      force: true
    }
  },
  logger: {
    quiet: false,
    stats: true
  },
    defineConstants: {
      API_BASE_URL: JSON.stringify('https://ozjs1558510.vicp.fun/api')
    },
  mini: {
    webpackChain (chain, webpack) {
      chain.module
        .rule('less')
        .test(/\.less$/)
        .use('style-loader')
        .loader('style-loader')
        .end()
        .use('css-loader')
        .loader('css-loader')
        .end()
        .use('less-loader')
        .loader('less-loader')
        .options({
          modifyVars: {
            // 在这里可以修改 Less 变量
            '@brand-primary': '#1890ff'
          },
          javascriptEnabled: true
        })
        .end()
    }
  },
  h5: {
    publicPath: '/',
    staticDirectory: 'static',
    postcss: {
      autoprefixer: {
        enable: true,
        config: {}
      },
      cssModules: {
        enable: false, 
        config: {
          namingPattern: 'module', 
          generateScopedName: '[name]__[local]___[hash:base64:5]'
        }
      }
    },
    devServer: {
      host: 'localhost',
      port: 10086,
      proxy: {
        '/api/1/upload': {//接口访问路径
            target: 'https://www.picgo.net/api/1/upload',  // 服务端域名
            changeOrigin: true,
            onProxyRes: (proxyRes, req, res) => {
              const allowedOrigin = 'http://localhost:10086';
              proxyRes.headers['Access-Control-Allow-Origin'] = allowedOrigin;
              proxyRes.headers['Access-Control-Allow-Credentials'] = 'true';
              proxyRes.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS';
              proxyRes.headers['Access-Control-Allow-Headers'] = 'Content-Type, X-API-Key';
              console.log('Modified Headers:', proxyRes.headers);
            },
            onProxyReq: (proxyReq, req, res) => {
            if (req.method === 'OPTIONS') {
              const allowedOrigin = 'http://localhost:10086';
              res.writeHead(200, {
                'Access-Control-Allow-Origin': allowedOrigin,
                'Access-Control-Allow-Credentials': 'true',
                'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
                'Access-Control-Allow-Headers': 'Content-Type, X-API-Key'
              });
              res.end();
            }
          },
        }
      }
      
    },
  }
} satisfies UserConfigExport<'webpack5'>